//
// Created by 46508 on 2024/5/21.
//

#ifndef LUAGRAPHICSENGINE_LUAUTIL_H
#define LUAGRAPHICSENGINE_LUAUTIL_H

extern "C"
{
#include "lauxlib.h"
}
#define __code(CODE) \
    do               \
    {                \
        CODE         \
    } while (0)

#define lua_push_const(L, TYPE, VALUE) __code( \
    lua_pushstring(L, #VALUE);                 \
    lua_push##TYPE(L, VALUE);                  \
    lua_settable(L, -3);)

#define lua_push_enum(L, ENUM) lua_push_const(L, integer, ENUM)

#define lua_push_enums(L, name, function) __code( \
    lua_pushstring(L, #name);                     \
    function;                                     \
    lua_settable(L, -3);)

#define GEN_LUA_TYPE_PUSH_FUNC(TYPE)                                         \
    static TYPE *luaL_check##TYPE(lua_State *L, int arg)                     \
    {                                                                        \
        TYPE *value = static_cast<TYPE *>(lua_touserdata(L, arg));           \
        if (!value)                                                          \
            luaL_typeerror(L, arg, #TYPE);                                   \
        return value;                                                        \
    }                                                                        \
    static TYPE *luaL_opt##TYPE(lua_State *L, int args, TYPE *def = nullptr) \
    {                                                                        \
        return luaL_opt(L, luaL_check##TYPE, args, def);                     \
    }                                                                        \
    inline static void lua_push##TYPE(lua_State *L, TYPE *v)                 \
    {                                                                        \
        lua_pushlightuserdata(L, v);                                         \
    }

#endif // LUAGRAPHICSENGINE_LUAUTIL_H
